package com.conduit.comnands;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.conduit.commands.prototype.AbstractCommand;
import com.conduit.enums.ResultCode;

public class SleepCommand extends AbstractCommand {
	
	public SleepCommand() {
		commandSyntax = "sleep\\s{2,}(.*)";
	}

	@Override
	public void executeWithArgs(String args) {
		if (!args.matches(commandSyntax)) {
			setResult("Error in Command's syntax (\"" + args + "\")");
			setResultCode(ResultCode.Error);
			return;
		}
		// get commnad's arguments
		Matcher matcher = Pattern.compile(commandSyntax).matcher(args);
		if (matcher.find()) {
			try{
			Thread.sleep(Long.parseLong(matcher.group(1)));
			setResult("Sleep for "+matcher.group(1));
			setResultCode(ResultCode.Successes);
			}catch (Exception e){
				setResult(e.getMessage());
				setResultCode(ResultCode.Error);
			}
		}
	}
	
	@Override
	public String getCommandName() {
		return "Sleep";
	}

}
